package com.panopset.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

import com.panopset.centralen.util.MathUtil;

/**
 * Test the Osgw object, by putting an Osgw in a window and doing
 * some simple animation.
 *
 * @author Karl Dinwiddie
 *
 */
public final class TestOsgw {

    /**
     * Test bounds location is 100, 200.
     */
    private static final Point TBL = new Point(100, 200);

    /**
     * Test bounds dimensions are 500, 600.
     */
    private static final Dimension TBD = new Dimension(500, 600);

    /**
     * x.
     */
    private int x = 0;

    /**
     * Test ball radius.
     */
    private static final int TBR = 12;

    /**
     * @param s Not used.
     */
    public static void main(final String... s) {
        new TestOsgw().go();
    }

    /**
     * Off screen graphics world.
     */
    private Osgw osgw;

    /**
     * @return Off screen graphics world.
     */
    Osgw getOsgw() {
        if (osgw == null) {
            osgw = new Osgw(TBL.x, TBL.y) {

                @Override
                public void paintCycle(final Graphics2D g) {
                    g.setColor(Color.yellow);
                    g.fillRect(0, 0, MathUtil.ONETHOUSAND,
                            MathUtil.ONETHOUSAND);
                    g.setColor(Color.blue);
                    g.drawOval(x++, MathUtil.ONEHUNDRED, TBR, TBR);
                    if (x > MathUtil.ONEHUNDRED) {
                        x = 0;
                    }
                }

            };
        }
        return osgw;
    }

    /**
     * Start animation.
     */
    public void go() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.addWindowListener(new WindowListener() {

            @Override
            public void windowActivated(final WindowEvent e) {
                // nothing to do here.
            }

            @Override
            public void windowClosed(final WindowEvent e) {
                Osgw.close();
            }

            @Override
            public void windowClosing(final WindowEvent e) {
                // nothing to do here.
            }

            @Override
            public void windowDeactivated(final WindowEvent e) {
                // nothing to do here.
            }

            @Override
            public void windowDeiconified(final WindowEvent e) {
                // nothing to do here.
            }

            @Override
            public void windowIconified(final WindowEvent e) {
                // nothing to do here.
            }

            @Override
            public void windowOpened(final WindowEvent e) {
                // nothing to do here.
            }

        });
        f.setTitle("Test OSGW");
        f.setBounds(TBL.x, TBL.y, TBD.width, TBD.height);
        f.setVisible(true);
        f.add(getOsgw().getPanel());
    }
}
